<?php
namespace app\admin\controller;

use app\admin\logic\SiteConfigLogic;
use app\admin\model\AuthModel;
use think\Controller;
use think\Request;
use think\Session;

class Base extends Controller
{
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $logic = new SiteConfigLogic();
        $logic->get_site_config();
        if(Session::get('admin') && Request::instance()->isAjax() === false){
            $this->_get_menu();
            $this->_get_nav();
            $this->assign('admin_name',Session::get('admin.user_name'));
        }
        $this->assign('version','v1');
    }

    private function _get_menu(){
        $where = array('is_menu'=>1);
        $admin = session('admin');
        if($admin['rule'] &&  !FOUNDER){
            $where['id'] = array('in',$admin['rule']);
        }
        $model = new AuthModel();
        $menuList = $model->selectData($where,'*','sort asc');
        $temp = array();
        $module = strtolower($this->request->module());
        $controller = strtolower($this->request->controller());
        $action = strtolower($this->request->action());
        usort($menuList,function($a,$b){
            if($a['pid'] == 0 && $a['sort'] < $b['sort']){
                return false;
            }else{
                return true;
            }
        });
        foreach ($menuList as $k=>$v){
            if($v['module'] == $module && $v['controller'] == $controller ){
                $v['is_select'] = 1;
            }else{
                $v['is_select'] = 0;
            }
            if($v['pid'] == 0){
                if(isset($temp[$v['id']]['parent']['is_select'])){
                    $v['is_select'] = $temp[$v['id']]['parent']['is_select'];
                }
                $temp[$v['id']]['parent'] = $v;
            }else{
                if(!isset($temp[$v['pid']])){
                    $temp[$v['pid']]['parent'] = [];
                }
                $temp[$v['pid']]['child'][] = $v;
                if($v['is_select'] == 1 &&
                    (!isset($temp[$v['pid']]['parent']['is_select']) ||
                    $temp[$v['pid']]['parent']['is_select'] == 0)){
                    $temp[$v['pid']]['parent']['is_select'] = 1;
                }
            }
        }
        $menuList = $temp;
        $this->assign('menuList',$menuList);
    }
    private function _get_nav(){
        $module = $this->request->module();
        $controller = $this->request->controller();
        $action = $this->request->action();
        $model = new AuthModel();
        $where = [];
        $where['module'] = strtolower($module);
        $where['controller'] = strtolower($controller);
        $where['action'] = strtolower($action);
        $auth = $model->findData($where);
        if($auth){
            $data = $model->get_nav_tree($auth['nav_pid'],[]);
            array_unshift($data,$auth);
            $this->assign('nav',array_reverse($data));
        }

    }
}
